package com.example.pceb.controller;

import com.example.pceb.entity.SchoolAdmin;
import com.example.pceb.entity.School;
import com.example.pceb.entity.Enterprise;
import com.example.pceb.entity.SchoolEnterpriseCooperation;
import com.example.pceb.result.Result;
import com.example.pceb.service.SchoolAdminService;
import com.example.pceb.service.SchoolService;
import com.example.pceb.service.EnterpriseService;
import com.example.pceb.service.SchoolEnterpriseCooperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/debug")
//@CrossOrigin(origins = "*")
public class DebugController {
    
    @Autowired
    private SchoolAdminService schoolAdminService;
    
    @Autowired
    private SchoolService schoolService;
    
    @Autowired
    private EnterpriseService enterpriseService;
    
    @Autowired
    private SchoolEnterpriseCooperationService cooperationService;
    
    @GetMapping("/school-admin-info")
    public Result<Map<String, Object>> getSchoolAdminInfo(@RequestParam String username) {
        Map<String, Object> result = new HashMap<>();
        
        // 获取学校管理员信息
        SchoolAdmin admin = schoolAdminService.getByUsername(username);
        result.put("admin", admin);
        
        if (admin != null && admin.getSchoolId() != null) {
            // 获取学校信息
            School school = schoolService.getById(admin.getSchoolId());
            result.put("school", school);
            
            if (school != null) {
                // 查询相关项目（状态不为"未立项"）
                List<SchoolEnterpriseCooperation> projects = cooperationService.getSchoolAdminPendingProjects(username);
                result.put("projects", projects);
                result.put("projectCount", projects.size());
            }
        }
        
        return Result.success(result);
    }
    
    @GetMapping("/enterprise-info")
    public Result<Map<String, Object>> getEnterpriseInfo(@RequestParam String username) {
        Map<String, Object> result = new HashMap<>();
        
        // 获取企业信息
        Enterprise enterprise = enterpriseService.getByUsername(username);
        result.put("enterprise", enterprise);
        
        if (enterprise != null) {
            // 查询相关项目（状态不为"未立项"）
            List<SchoolEnterpriseCooperation> projects = cooperationService.getEnterpriseAdminPendingProjects(username);
            result.put("projects", projects);
            result.put("projectCount", projects.size());
        }
        
        return Result.success(result);
    }
    
    @GetMapping("/all-pending-projects")
    public Result<List<SchoolEnterpriseCooperation>> getAllPendingProjects() {
        // 查询所有待立项项目
        List<SchoolEnterpriseCooperation> projects = cooperationService.listByPublisherType("学校", "待立项");
        projects.addAll(cooperationService.listByPublisherType("企业", "待立项"));
        return Result.success(projects);
    }
} 